<template>
  <div class="home-contaienr">
    <el-container>
      <!-- 头部区域start -->
      <el-header>
        <div class="home_header">
          <div class="home_header_logo">
            <img src="@/assets/logo.png" alt="" />
            <span>电商后台管理系统</span>
          </div>
          <div class="home_header_btn">
            <el-button type="info" @click="quitHome">退出</el-button>
          </div>
        </div>
      </el-header>
      <!-- 头部区域end -->

      <el-container>
        <el-aside :style="collspan" ref="asideRef">
          <div class="toggle_button" @click="toggle">
            |||
          </div>
          <el-menu
            :default-active="activePath"
            router
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
          >
            <el-submenu
              :index="'/' + item.path"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavState('/' + subItem.path)"
              >
                <i class="el-icon-menu"></i>
                <span slot="title">{{ subItem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
/* api调用 */
import { getMenuListApi } from '@/api/HomeApi/HomeApi.js'

export default {
  name: 'Home',
  data() {
    return {
      //获取到的菜单栏数据
      menuList: [],
      //折叠按钮
      isCollapse: false,
      //被激活的index
      activePath: '',
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    /* 退出按钮 */
    quitHome() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    /* 请求菜单栏数据 */
    async getMenuList() {
      const { data: res } = await getMenuListApi()
      if (res.meta.status !== 200) {
        return this.$message.error('获取信息失败')
      } else {
        this.menuList = res.data
        // console.log(this.menuList)
      }
    },

    /* 折叠菜单栏 */
    toggle() {
      this.isCollapse = !this.isCollapse
    },

    /* 保存链接状态即index值 */
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
  computed: {
    collspan() {
      if (this.isCollapse === true) {
        return 'width:64px'
      } else {
        return 'width:200px'
      }
    },
  },
}
</script>

<style lang="less" scoped>
.home-contaienr {
  height: 100%;
}
.el-container {
  height: 100%;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eee;
}
.el-header {
  background-color: #373d41;
  height: 100%;
}
.home_header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .home_header_logo {
    display: flex;
    align-items: center;
  }
  img {
    width: 60px;
    height: 60px;
  }
  span {
    font-size: 24px;
    color: #fff;
    margin-left: 5px;
  }
  .el-button {
    margin-right: 30px;
    border-radius: 10px;
  }
}
.el-menu {
  border-right: none;
}
.toggle_button {
  background-color: #4a5064;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  height: 24px;
  line-height: 24px;
  cursor: pointer;
}
</style>
